//
//  RefreshView.m
//  HLZhiHuDaily
//
//  Created by 胡林 on 16/3/3.
//  Copyright © 2016年 胡林. All rights reserved.
//

#import "RefreshView.h"

@implementation RefreshView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:self.indicatorView];
        CGFloat radius = MIN(frame.size.width, frame.size.height)/2-3;
        self.grayCircleLayer = [[CAShapeLayer alloc] init];
        self.grayCircleLayer.lineWidth = 0.5f;
        self.grayCircleLayer.strokeColor = [UIColor clearColor].CGColor;
        self.grayCircleLayer.fillColor = [UIColor grayColor].CGColor;
        self.grayCircleLayer.opacity = 0.f;
        self.grayCircleLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(self.width/2 - radius, self.height / 2 - radius, 2*radius, 2*radius)].CGPath;
        
        [self.layer addSublayer:self.grayCircleLayer];
        self.whiteCircleLayer = [[CAShapeLayer alloc] init];
        self.whiteCircleLayer.lineWidth = 2.f;
        self.whiteCircleLayer.strokeColor = [UIColor whiteColor].CGColor;
        self.whiteCircleLayer.fillColor = [UIColor clearColor].CGColor;
        self.whiteCircleLayer.strokeEnd = 0.f;
        self.whiteCircleLayer.opacity = 0.f;
        self.whiteCircleLayer.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width / 2, self.height/2) radius:radius startAngle:M_PI_2 endAngle:M_PI * 5/2 clockwise:YES].CGPath;
        [self.layer addSublayer:self.whiteCircleLayer];
    
    
    }return self;
}
//重绘的进展
- (void)redrawFromProgress:(CGFloat)progress{
    if (progress>0) {
        _whiteCircleLayer.opacity = 1.f;
        _grayCircleLayer.opacity = 1.f;
    }else if (progress <= 0){
        _whiteCircleLayer.opacity = 0.f;
        _grayCircleLayer.opacity = 0.f;
    }
    _whiteCircleLayer.strokeEnd = progress;
}
//开始动画
- (void)startAnimation {
    [_indicatorView startAnimating];
}
//结束动画
- (void)stopAnimation {
    [_indicatorView stopAnimating];
}



@end
